﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data; 
using SWIET.MimitControl;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace XmlAppliction
{
    public class Globes
    { 

        /// <summary>产品完成结果字典</summary>
        public static Dictionary<string, string> m_dictResult = new Dictionary<string, string>();

        /// <summary> 用户ID </summary>
        public static string m_UserID = "";
        /// <summary> 用户名 </summary>
        public static string m_UserName = "";
        public static string m_UserPassword = "";
        public static string m_DatabaseName = "";
        
        /// <summary>
        /// 用户登录操作
        /// </summary>
        /// <returns>成功=true,失败=false</returns>
        public static bool DoLogin()
        {
            bool bRet = false;
            int iLoginTimes = 0;
            M_Comm_LoginForm LoginForm=null;
            // 登录窗
            if (m_UserID == "")
            {
                LoginForm = new M_Comm_LoginForm();
                LoginForm.软件标题.Text = "综合业务统计软件";
                //隐藏选项
                LoginForm.button1.Visible = false;
                //角色设置
                LoginForm.M_IS_Have_Group_Infor = false;
            }

            
            while (iLoginTimes < 3)
            {
                if (LoginForm !=null && LoginForm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    return false;
                }


                string sql;
                if (LoginForm !=null)
                {
                    sql = string.Format("select * from tb_usr_workuser t where t.workuser= '{0}' and t.password='{1}'", LoginForm.M_UserName.Replace('\'', '\"'), LoginForm.M_PassWord.Replace('\'', '\"'));
                }
                else
                {
                    sql = string.Format("select * from tb_usr_workuser t where t.workuser= '{0}' and t.password='{1}'", m_UserID.Replace('\'', '\"'), m_UserPassword.Replace('\'', '\"'));
                }
                try
                {
                    DataSet ds = DBMgr.DbHelperOra.Query(sql);
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)//登录成功
                    {
                        break; 
                    }
                    else
                    {
                        MessageBox.Show("请正确输入用户名和密码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //cmd模式下提示一次
                        if (LoginForm == null)
                        {
                            iLoginTimes = 4;
                            break;
                        }
                        iLoginTimes++;
                    }

                }
                catch
                {
                    MessageBox.Show("数据库连接错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //cmd模式下提示一次
                    if (LoginForm == null)
                    {
                        iLoginTimes = 4;
                        break;
                    }
                }
                
            }
            if (LoginForm !=null  && bRet)
            {
                LoginForm.M_Save_Login_Info();
                LoginForm.Dispose();
            }

            if (iLoginTimes < 3)		// 表明登录成功
            {
                bRet = true;
            }
            else if (LoginForm !=null)
            {
                MessageBox.Show("连续三次登录失败，软件即将退出！", "错误");
            }
            return bRet;
        }

        public static string GetLocalIP()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {

                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                foreach (IPAddressInformation any in adapterProperties.UnicastAddresses)
                {
                    return any.Address.ToString();
                }
            }
            return "";
        }

        public static bool M_ValidateIP(string SoftName, string IP)
        {
            try
            {
                string sql = string.Format("select IPQS,IPJS from tb_cfg_dlyz where RJMC='{0}'",SoftName);
                System.Data.DataSet ds = new System.Data.DataSet();
                ds = DBMgr.DbHelperOra.Query(sql); 
                long iplong = System.Net.IPAddress.Parse(IP).Address;
                foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                {
                    long ipqs = System.Net.IPAddress.Parse(dr[0].ToString()).Address;
                    long ipjs = System.Net.IPAddress.Parse(dr[1].ToString()).Address;
                    if (iplong >= ipqs && iplong<=ipjs)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static string M_GetResultString(string code)
        {
            string[] s1 = code.Split(',');
            try
            {
                string s = "";
                foreach (string s2 in s1)
                {
                    s = s + Globes.m_dictResult[s2] + ",";
                }
                s = s.Remove(s.Length - 1);
                return s;
            }
            catch
            {
                return code;
            }
        }

    }
}
